package com.tz.cartelo.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tz.cartelo.R;
import com.tz.cartelo.activity.LoginActivity;
import com.tz.cartelo.activity.ManagerSetActivity;
import com.tz.cartelo.activity.ManagerStockActivity;
import com.tz.cartelo.activity.MyOrderActivity;
import com.tz.cartelo.activity.SystemMessageActivity;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.result.ResultMessageNum;
import com.tz.cartelo.util.CommonUtils;
import com.tz.cartelo.util.EventBusSendMessage;
import com.tz.cartelo.view.RoundImageView;
import com.tz.common.util.LogUtil;
import com.tz.common.util.PreferencesUtils;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

import static android.app.Activity.RESULT_OK;

/**
 * Created by ok on 2016/9/12.
 */
public class ManageCenterFragment extends DataLoadFragment implements View.OnClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {
    private static final String TAG = "ManageCenterFragment";
    private static final int USER_MESSAGE = 1;
    private static final int MESSAGE = 2;
    private BGARefreshLayout mRefreshLayout;
    private View mManagerCenterSystemMessageView, mManagerCenterSetView, mManagerCenterOrderView, mManagerGeneralAgentView, mManagerAgentView, mManagerJadeFriendView,
            mManagerMonthSalesVolumeView, mManagerDealOrderView, mManagerAlreadyDealOrderView;
    private TextView mManagerCenterSystemMessageTextRight, mManagerCenterSystemMessageTextDot;
    private Api mApi;

    @Override
    protected int getLayoutResId() {
        return R.layout.main_manage_center_fragment;
    }

    @Override
    protected void init(View view) {
        EventBus.getDefault().register(this);

        mRefreshLayout = (BGARefreshLayout) view.findViewById(R.id.my_fresh_layout);
        mRefreshLayout.setDelegate(this);
        CommonUtils.setBGARefreshLayout(mRefreshLayout, getActivity());

//        LinearLayout managerHeadBgLayout =(LinearLayout) view.findViewById(R.id.manager_head_bg_layout);
//        managerHeadBgLayout.setBackgroundResource(R.mipmap.ic_manager_head_bg);
        TextView managerNameText = (TextView) view.findViewById(R.id.manager_name_text);
        managerNameText.setText("用户名：" + PreferencesUtils.getString(Key.PER_USER_NICK, ""));
        RoundImageView managerHeadImg = (RoundImageView) view.findViewById(R.id.manager_head_img);
        ImageLoader.getInstance().displayImage(PreferencesUtils.getString(Key.PER_USER_HEAD, ""), managerHeadImg, CommonUtils.optionsHead());

        View managerCenterStockView = view.findViewById(R.id.manager_center_stock_view);
        TextView managerCenterStockText = (TextView) managerCenterStockView.findViewById(R.id.item_my_text);
        ImageView managerCenterStockImg = (ImageView) managerCenterStockView.findViewById(R.id.item_my_image);
        managerCenterStockImg.setImageResource(R.mipmap.ic_manager_center_stock);
        managerCenterStockText.setText(R.string.manager_center_stock);
        managerCenterStockView.setOnClickListener(this);

        mManagerCenterSystemMessageView = view.findViewById(R.id.manager_center_system_message_view);
        TextView mManagerCenterSystemMessageText = (TextView) mManagerCenterSystemMessageView.findViewById(R.id.item_my_text);
        ImageView mManagerCenterSystemMessageImg = (ImageView) mManagerCenterSystemMessageView.findViewById(R.id.item_my_image);
        mManagerCenterSystemMessageTextRight = (TextView) mManagerCenterSystemMessageView.findViewById(R.id.item_my_text_right);
        mManagerCenterSystemMessageTextDot = (TextView) mManagerCenterSystemMessageView.findViewById(R.id.item_my_text_dot);
        mManagerCenterSystemMessageImg.setImageResource(R.mipmap.ic_manager_center_system);
        mManagerCenterSystemMessageText.setText(R.string.manager_center_system_message);
        mManagerCenterSystemMessageView.setOnClickListener(this);

        mManagerCenterSetView = view.findViewById(R.id.manager_center_set_view);
        TextView mManagerCenterSetText = (TextView) mManagerCenterSetView.findViewById(R.id.item_my_text);
        ImageView mManagerCenterSetImg = (ImageView) mManagerCenterSetView.findViewById(R.id.item_my_image);
        mManagerCenterSetImg.setImageResource(R.mipmap.ic_manager_center_set);
        mManagerCenterSetText.setText(R.string.manager_center_system_set);
        mManagerCenterSetView.setOnClickListener(this);

        mManagerCenterOrderView = view.findViewById(R.id.manager_center_order_view);
        TextView mManagerCenterOrderText = (TextView) mManagerCenterOrderView.findViewById(R.id.item_my_text);
        ImageView mManagerCenterOrderImg = (ImageView) mManagerCenterOrderView.findViewById(R.id.item_my_image);
        mManagerCenterOrderImg.setImageResource(R.mipmap.ic_manager_center_order);
        mManagerCenterOrderText.setText(R.string.manager_center_system_order);
        mManagerCenterOrderView.setOnClickListener(this);

        view.findViewById(R.id.manager_center_sign_out).setOnClickListener(this);
    }

    @Override
    protected void initTitle(View view) {
    }

    @Override
    protected void initData() {
//        if (PreferencesUtils.getBoolean(Key.PER_LOGIN, false)) {
        mApi = Api.MESSAGE_NUM;
        loadData(mApi, false);
//        } else {
//            showNoData();
//        }
    }

    @Override
    protected void initParams(DataParams params) {
        switch (mApi) {
            case MESSAGE_NUM:
                params.addParam("userId", PreferencesUtils.getString(Key.PER_USER_ID, ""));
                break;
            default:
                break;
        }

    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case MESSAGE_NUM:
                mRefreshLayout.endRefreshing();
                mRefreshLayout.endRefreshing();

                ResultMessageNum resultMessageNum = fromJson(response, ResultMessageNum.class);
                if (!resultMessageNum.isSuccess()) {
                    showToast(resultMessageNum.getMessage());
                    return;
                }
                if (CommonUtils.isEmpty(resultMessageNum.getMessageNum().getInformationNum())) {
                    mManagerCenterSystemMessageTextDot.setVisibility(View.GONE);
                } else {
                    int messageNum = Integer.parseInt(resultMessageNum.getMessageNum().getInformationNum());
                    if (messageNum > 0)
                        mManagerCenterSystemMessageTextDot.setVisibility(View.VISIBLE);
                    else
                        mManagerCenterSystemMessageTextDot.setVisibility(View.GONE);
                }

                break;
            default:
                break;
        }
    }

    @Override
    protected void errorResponse() {
        mRefreshLayout.endRefreshing();
        mRefreshLayout.endLoadingMore();
    }

    @Override
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.manager_center_stock_view://库存统计
                switchActivity(ManagerStockActivity.class, null, Key.CLASSIFY_STOCK_TOTAL);
                break;
            case R.id.manager_center_system_message_view://系统通知
                switchActivityForResult(SystemMessageActivity.class, MESSAGE, null);
                break;
            case R.id.manager_center_set_view://管理设置
                switchActivity(ManagerSetActivity.class, null);
                break;
            case R.id.manager_center_order_view://我的订单
                switchActivity(MyOrderActivity.class, null);
                break;
            case R.id.manager_center_sign_out://退出登录
                showOutDialog();
                break;
            default:
                break;
        }
    }

    private void showOutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.system_prompt);
        builder.setMessage(R.string.manager_center_make_sure_sign_out);
        builder.setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                signOut();
            }
        });
        builder.setNegativeButton(R.string.cancel, null);
        builder.show();
    }

    private void signOut() {
        PreferencesUtils.putBoolean(Key.PER_LOGIN, false);
        PreferencesUtils.clearPre();//清空本地保存数据
        showToast(R.string.manager_center_sign_out_succeed);
        switchActivityAndFinish(LoginActivity.class, null);
    }

    /*
    private void showUserType(List<ActUserType> userTypeList) {
        for (ActUserType actUserType : userTypeList) {
            switch (actUserType.getUserType()) {
                case Key.USER_TYPE_JADE_FRIEND://翠友-3
                    mManagerJadeFriendView.setVisibility(View.VISIBLE);
                    mManagerJadeFriendTextRight.setText(actUserType.getCount() + "");
                    break;
                case Key.USER_TYPE_AGENT://代理-4
                    mManagerAgentView.setVisibility(View.VISIBLE);
                    mManagerAgentTextRight.setText(actUserType.getCount() + "");
                    break;
                case Key.USER_TYPE_GENERAL_AGENT://总代理-5
                    mManagerGeneralAgentView.setVisibility(View.VISIBLE);
                    mManagerGeneralAgentTextRight.setText(actUserType.getCount() + "");
                    break;
                case Key.USER_TYPE_BRANCH_COMPANY://分公司-6
                    mManagerCenterOrderView.setVisibility(View.VISIBLE);
                    mManagerCenterOrderTextRight.setText(actUserType.getCount() + "");
                    break;
                case Key.USER_TYPE_GENERAL_COMPANY://总公司-7 TODO
                    mManagerCenterSetView.setVisibility(View.VISIBLE);
                    mManagerCenterSystemMessageTextRight.setText(actUserType.getCount() + "");
                    mManagerMonthSalesVolumeView.setVisibility(View.GONE);
                    break;
                case Key.USER_TYPE_SUPPLIER://供应商-2
                    mManagerCenterSystemMessageView.setVisibility(View.VISIBLE);
                    mManagerMonthSalesVolumeView.setVisibility(View.GONE);
                    mManagerDealOrderView.setVisibility(View.GONE);
                    mManagerAlreadyDealOrderView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }
    }

    private void showUserType(int userType) {
        switch (userType) {
            case Key.USER_TYPE_JADE_FRIEND://翠友-3
                mManagerJadeFriendView.setVisibility(View.VISIBLE);
                break;
            case Key.USER_TYPE_AGENT://代理-4
                mManagerAgentView.setVisibility(View.VISIBLE);
                break;
            case Key.USER_TYPE_GENERAL_AGENT://总代理-5
                mManagerGeneralAgentView.setVisibility(View.VISIBLE);
                break;
            case Key.USER_TYPE_BRANCH_COMPANY://分公司-6
                mManagerCenterOrderView.setVisibility(View.VISIBLE);
                break;
            case Key.USER_TYPE_GENERAL_COMPANY://总公司-7 TODO
                mManagerCenterSetView.setVisibility(View.VISIBLE);
                mManagerMonthSalesVolumeView.setVisibility(View.GONE);
                break;
            case Key.USER_TYPE_SUPPLIER://供应商-2
                mManagerCenterSystemMessageView.setVisibility(View.VISIBLE);
                mManagerMonthSalesVolumeView.setVisibility(View.GONE);
                mManagerDealOrderView.setVisibility(View.GONE);
                mManagerAlreadyDealOrderView.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

*/
    private void showNoData() {
        mManagerCenterSystemMessageView.setVisibility(View.GONE);
        mManagerGeneralAgentView.setVisibility(View.GONE);
        mManagerAgentView.setVisibility(View.GONE);
        mManagerJadeFriendView.setVisibility(View.GONE);
        mManagerCenterOrderView.setVisibility(View.GONE);
        mManagerCenterSetView.setVisibility(View.GONE);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case USER_MESSAGE:
                    mApi = Api.GET_ACT_USER_TYPE;
                    loadData(mApi, true);
                    break;
                case MESSAGE:
                    mApi = Api.MESSAGE_NUM;
                    loadData(mApi, false);
                    break;
                default:
                    break;
            }
        }
    }

    // 接收方法,默认的tag,执行在UI线程
    @Subscriber
    private void updateUser(EventBusSendMessage eventBusSendMessage) {
        switch (eventBusSendMessage.message) {
            case Key.GET_MESSAGE:
                mApi = Api.MESSAGE_NUM;
                loadData(mApi, false);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        mApi = Api.MESSAGE_NUM;
        loadData(mApi, true);
        mRefreshLayout.endRefreshing();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        mRefreshLayout.endLoadingMore();
        return false;
    }
}
